package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.EditText;

import com.sskj.cndragon.R;
import com.sskj.cndragon.component.DaggerUserDataComponent;
import com.sskj.cndragon.http.HttpCommon;
import com.sskj.cndragon.http.HttpConfig;
import com.sskj.cndragon.presenter.NickNameActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class NickNameActivity extends BaseActivity<NickNameActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.et_nickname)
    EditText etNickname;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nick_name;
    }

    @Override
    public NickNameActivityPresenter getPresenter() {
        return new NickNameActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setRightButtonOnClickListener(view -> {
            if (isEmpty(etNickname)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.appQingshurunicheng));
                return;
            }
            if (userData != null) {
                mPresenter.editNickName(String.valueOf(userData.getId()), getEtext(etNickname));
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            if (userData != null) {
                etNickname.setText(userData.getNickname());
            }
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NickNameActivity.class);
        context.startActivity(intent);
    }

    public void editTextSuccess(String nickName) {
        userData.setNickname(nickName);
        userViewModel.update(userData);
        finish();
    }
}
